<template>
  <!-- 定义布局容器 -->
  <el-container class="home-container">

    <!-- 定义头标签 -->
    <el-header>
      <div>
        <i class="el-icon-s-unfold" style="margin-left: 5px;"></i>
        <span>ZPK电影院会员管理系统</span>
      </div>
      <div class="avatar_box">
        <el-avatar icon="el-icon-s-custom" :style="adminForm.avatarClass" :size="40"/>
        <el-dropdown @command="logout" placement="bottom">
          <span class="el-dropdown" v-text="adminForm.adminName"/>
          <i class="el-icon-caret-bottom el-icon--right"></i>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>切换账号</el-dropdown-item>
            <el-dropdown-item>退出</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>

    <!-- 定义中间区域-->
    <el-container>
      <!-- 当打开左侧菜单时 宽度为200, 当不打开时为默认值-->
      <el-aside :width="isCollapse ? '100px' : '240px' ">
        <!-- 这是左侧菜单-->
        <!--
          background-color 定义背景色
          text-color="#fff"  定义文字颜色
          active-text-color="#4094ff" 选中元素的颜色
          unique-opened 是否只保持一个子菜单的展开
          collapse      是否水平折叠收起菜单（仅在 mode 为 vertical 时可用）
          collapse-transition	是否开启折叠动画 默认不要展现
          router 是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转
        -->
        <el-menu background-color="#000035" text-color="#fff" active-text-color="#4094ff" unique-opened
        :collapse="isCollapse" :collapse-transition="isCollapseTransition" router  :default-active="defaultActive">

          <!-- 定义一级菜单 -->
          <el-menu-item index="/home" @click="defaultActiveMenu(homePath)">
            <template slot="title">
              <i class="el-icon-s-home"></i>
              <span>系统首页</span>
            </template>
          </el-menu-item>
          <el-submenu :index="menu.id+''" v-for="menu in menuList" :key="menu.id">
            <!-- 定义一级菜单模版 -->
            <template slot="title">
              <!-- 定义左侧图标-->
              <i :class="menuIcon[menu.id]"></i>
              <!-- 定义菜单名称-->
              <span>{{menu.name}}</span>
            </template>
            <!-- 定义二级菜单 -->
            <el-menu-item :index="childrenMenu.path" v-for="childrenMenu in menu.children" :key="childrenMenu.id"
            @click="defaultActiveMenu(childrenMenu.path),addRightList(childrenMenu.name)">
              <template slot="title">
                <i :class="menuIcon[childrenMenu.id]"></i>
                <span>{{childrenMenu.name}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 定义主页面结构-->
      <el-main>
        <el-row>
          <el-col :span="22">
            <el-tag effect="dark" size="medium" style="margin-left: 15px;">系统首页</el-tag>
            <el-tag closable  effect="dark" size="medium" :key="right"
            v-for="right in rightList"  @close="handleClose(right)"
            style="margin-left: 10px;">{{right}}</el-tag>
          </el-col>
        <el-col :span="2">
          <el-dropdown  @command="handleCommand" placement="bottom">
            <el-button type="primary" size="mini" style="margin-left: 17px;">
              标签选项<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="系统首页">系统首页</el-dropdown-item>
              <el-dropdown-item v-for="right in rightList" :command="composeValue(right)">{{right}}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
        <!-- 定义路由展现页面-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    //初始化函数
    created() {
      //动态获取左侧菜单信息
      this.getMenuList()
      //设定模式选中按钮
      this.adminForm=JSON.parse(window.sessionStorage.getItem("admin"))
      this.defaultActive = window.sessionStorage.getItem("activeMenu")
    },
    data() {
      return {
        adminForm:{},
        homePath:'/home',
        rightList:[],
        menuList: [],
        menuIcon: {
          '1': 'el-icon-s-custom',
          '2': 'el-icon-suitcase',
          '3': 'el-icon-document-copy',
          '4': 'el-icon-s-cooperation',
          '5': 'el-icon-pie-chart',
          '6': 'el-icon-user',
          '7': 'el-icon-bank-card',
          '8': 'el-icon-coin',
          '9': 'el-icon-shopping-cart-full',
          '10': 'el-icon-copy-document',
          '11': 'el-icon-s-help',
          '12': 'el-icon-present',
          '13': 'el-icon-s-data'
        },
        //定义是否折叠
        isCollapse: false,
        //是否展现折叠动态效果
        isCollapseTransition: false,
        //定义默认高亮
        defaultActive: ''
      }
    },
    methods: {
      logout() {
        //1.删除session中的数据
        window.sessionStorage.clear()
        //2.用户访问登录页面
        this.$router.push('/login')
      },
      async getMenuList() {
        const {data: result} =  await this.$http.get('/rights/getRightsList')
        if(result.status !== 200) return this.$message.error("左侧菜单查询失败")
        this.menuList = result.data
      },
      addRightList(rightName){
        if(this.rightList.indexOf(rightName)===-1){
          this.rightList.push(rightName)
        }
      },
      async handleClose(right){
        let name=right
        this.rightList.splice(this.rightList.indexOf(right),1)
        const {data:result} = await this.$http.get(`rights/getPath?name=${name}`)
        if(this.defaultActive===result.data){
          this.defaultActiveMenu('/home')
          this.$router.push('/home')
          this.$message.success("跳转到系统首页")
        }
      },
      async handleCommand(command){
        if(command==="系统首页"){
          if(this.defaultActive==='/home') {
            this.$message.success(`跳转到系统首页!`)
            return
          }
          this.defaultActiveMenu('/home')
          this.$router.push('/home')
          this.$message.success(`跳转到系统首页!`)
          return
        }
        let name=command.right
        const {data:result} = await this.$http.get(`rights/getPath?name=${name}`)
        if(result.status !== 200) return this.$message.error(`跳转失败!`)
        this.$message.success(`跳转到${name}!`)
        if(this.defaultActive===result.data) return
        this.defaultActiveMenu(result.data)
        this.$router.push(result.data)
      },
      composeValue(right){
        return {
          'right':right
        }
      },
      //设定左侧折叠展开效果
      collspseClick() {
        this.isCollapse = !this.isCollapse
      },
      defaultActiveMenu(activeMenu){
        //为了实现返回之后的选中效果,应该将数据保存到第三方中sessionStory
        window.sessionStorage.setItem("activeMenu",activeMenu)
        this.defaultActive = activeMenu
      }
    }
  }
</script>

<!-- 防止样式重叠 -->
<style lang="less" scoped>
  .avatar_box {
    margin-right: 30px;
    .el-dropdown{
      margin-left: 10px;
      font-size: 12px;
      color: white;
    }
  }
  .el-header {
    background-color: #000028;
    display: flex; //灵活的盒子容器
    justify-content: space-between; //左右对齐
    align-items: center; //文本元素居中对齐
    color: #FFFFFF; //设定字体颜色
    padding-left: 1px;

    div {
      display: flex;
      align-items: center;

      span {
        margin-left: 15px;
        font-size: 25px; //设定字体高度
      }
    }
  }
  .el-aside {
    background-color: #000035;

    .el-menu {
      border-right: none;
    }
  }

  .el-main {
    background-color: #EEEEEE;
  }

  .home-container {
    height: 100%;
    width: 100%;
  }

  .iconfont {
    margin-right: 8px;
  }

  //定义折叠项
  .leftCollapse {
    //设定背景色
    background-color: #2C3E50;
    //定义字体大小
    font-size: 10px;
    //定义行高
    line-height: 30px;
    //定义颜色
    color: #FFFFFF;
    //设置居中
    text-align: center;
    //设定字符间距
    letter-spacing: 4px;
    //鼠标放上之后设置为小手
    cursor: pointer;
  }
</style>
